/*----------------------------------------------------------------------------

	Copyright (c) 2006, Creator of the Cant Project.
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	+ Redistributions of source code must retain the above copyright notice,
	this list of conditions and the following disclaimer.

	+ Redistributions in binary form must reproduce the above copyright notice,
	this list of conditions and the following disclaimer in the documentation
	and/or other materials provided with the distribution.

	+ Neither the name of the Creator of the Cant Project nor the names of its
	contributors may be used to endorse or promote products derived from this
	software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------------*/

#include	"projecttest.hpp"



ProjectTest::ProjectTest(void )	: TestCase("ProjectTest" )
{
	addMethod(testGetProperty );
	addMethod(testGetProperties );
	addMethod(testGetTarget );
	addMethod(testGetTargets );
	addMethod(testOperatorIndexer );
	addMethod(testEvaluate );
	addMethod(testGetBaseDirectory );
	addMethod(testGetDefaultTarget );
	addMethod(testGetDescription );
	addMethod(testGetName );
	
	m_project			= 0x0;
	m_projectPrev		= 0x0;
}


void	ProjectTest::setup(void )
{
	//MW:	Call 'createObject' for each test - note that the same pointer
	//		should be returned for every call so this will be checked...
	m_project			= Project::createProject("test/testbuild.xml" );
	assertPost((m_projectPrev == 0x0 ) || (m_project = m_projectPrev ), "non-match for project pointer" );
	
	m_projectPrev		= m_project;
}


void	ProjectTest::teardown(void )
{
	//MW:	Don't delete the project pointer - it will be automatically
	//		released when the static project-list goes out of scope.
}


void	ProjectTest::testGetProperty(TestCase* a_test )
{
	ProjectTest*		pTest			= static_cast<ProjectTest*>(a_test );
	pTest->assertPre(pTest->m_project != 0x0, "ProjectTest:: project pointer not initialsed" );

	pTest->assertTrue(pTest->m_project->getProperty("one" ) == "1", "getProperty('one')" );
	pTest->assertTrue(pTest->m_project->getProperty("two" ) == "2", "getProperty('two')" );
	pTest->assertTrue(pTest->m_project->getProperty("three" ) == "3", "getProperty('three')" );
	
	pTest->assertTrue(pTest->m_project->getProperty("uno:zap" ) == "zappy", "getProperty('uno:zap')" );
	pTest->assertTrue(pTest->m_project->getProperty("uno:kapow" ) == "kapowy", "getProperty('uno:kapow')" );

	pTest->assertTrue(pTest->m_project->getProperty("duo:little" ) == "sid", "getProperty('uno:little')" );
	pTest->assertTrue(pTest->m_project->getProperty("duo:large" ) == "eddie", "getProperty('uno:large')" );
	pTest->assertTrue(pTest->m_project->getProperty("duo:cannon" ) == "tommy", "getProperty('uno:cannon')" );
	pTest->assertTrue(pTest->m_project->getProperty("duo:ball" ) == "bobby", "getProperty('uno:ball')" );
}


void	ProjectTest::testGetProperties(TestCase* a_test )
{
	ProjectTest*			pTest			= static_cast<ProjectTest*>(a_test );
	pTest->assertPre(pTest->m_project != 0x0, "ProjectTest:: project pointer not initialsed" );
	
	pTest->assertTrue(pTest->m_project->getProperties().size() == 6, "number of properties" );

	pTest->assertTrue(Project::getProjectList().get("uno" )->getProperties().size() == 2, "number of properties(uno)" );
	pTest->assertTrue(Project::getProjectList().get("duo" )->getProperties().size() == 4, "number of properties(duo)" );
}


void	ProjectTest::testGetTarget(TestCase* a_test )
{
	ProjectTest*		pTest			= static_cast<ProjectTest*>(a_test );
	pTest->assertPre(pTest->m_project != 0x0, "ProjectTest:: project pointer not initialsed" );

	pTest->assertTrue(pTest->m_project->getTarget("alpha" ).getName() == "alpha", "getTarget('alpha')" );
	pTest->assertTrue(pTest->m_project->getTarget("beta" ).getName() == "beta", "getTarget('beta')" );
	pTest->assertTrue(pTest->m_project->getTarget("gamma" ).getName() == "gamma", "getTarget('gamma')" );

	pTest->assertTrue(pTest->m_project->getTarget("uno:target1" ).getName() == "target1", "getTarget('uno:target1')" );
	pTest->assertTrue(pTest->m_project->getTarget("uno:target2" ).getName() == "target2", "getTarget('uno:target2')" );

	pTest->assertTrue(pTest->m_project->getTarget("duo:target1" ).getName() == "target1", "getTarget('uno:target1')" );
	pTest->assertTrue(pTest->m_project->getTarget("duo:target2" ).getName() == "target2", "getTarget('uno:target2')" );
	pTest->assertTrue(pTest->m_project->getTarget("duo:target3" ).getName() == "target3", "getTarget('uno:target3')" );
	pTest->assertTrue(pTest->m_project->getTarget("duo:target4" ).getName() == "target4", "getTarget('uno:target4')" );
	pTest->assertTrue(pTest->m_project->getTarget("duo:target5" ).getName() == "target5", "getTarget('uno:target5')" );
}


void	ProjectTest::testGetTargets(TestCase* a_test )
{
	ProjectTest*			pTest			= static_cast<ProjectTest*>(a_test );
	pTest->assertPre(pTest->m_project != 0x0, "ProjectTest:: project pointer not initialsed" );
	
	pTest->assertTrue(pTest->m_project->getTargets().size() == 3, "number of targets" );

	pTest->assertTrue(Project::getProjectList().get("uno" )->getTargets().size() == 4, "number of targets(uno)" );
	pTest->assertTrue(Project::getProjectList().get("duo" )->getTargets().size() == 5, "number of targets(duo)" );
}


void	ProjectTest::testOperatorIndexer(TestCase* a_test )
{
	try
	{
		ProjectTest*		pTest			= static_cast<ProjectTest*>(a_test );
		pTest->assertPre(pTest->m_project != 0x0, "ProjectTest:: project pointer not initialsed" );
	
		pTest->assertTrue((*pTest->m_project)["alpha"].getName() == "alpha", "indexer['alpha']" );
		pTest->assertTrue((*pTest->m_project)["beta"].getName() == "beta", "indexer['beta']" );
		pTest->assertTrue((*pTest->m_project)["gamma"].getName() == "gamma", "indexer['gamma']" );
	}
	catch(TagException )
	{
		a_test->assertTrue(false, "indexer[]" );
	}
}


void	ProjectTest::testEvaluate(TestCase* a_test )
{
	ProjectTest*			pTest			= static_cast<ProjectTest*>(a_test );
	pTest->assertPre(pTest->m_project != 0x0, "ProjectTest:: project pointer not initialsed" );
	
	pTest->assertTrue(pTest->m_project->evaluate("${one}${two}${three}" ) == "123", "evaluate()" );
}


void	ProjectTest::testGetBaseDirectory(TestCase* a_test )
{
	ProjectTest*			pTest			= static_cast<ProjectTest*>(a_test );
	pTest->assertPre(pTest->m_project != 0x0, "ProjectTest:: project pointer not initialsed" );

	pTest->assertTrue(pTest->m_project->getBaseDirectory() == ".", "getBaseDirectory()" );
}


void	ProjectTest::testGetDefaultTarget(TestCase* a_test )
{
	ProjectTest*			pTest			= static_cast<ProjectTest*>(a_test );
	pTest->assertPre(pTest->m_project != 0x0, "ProjectTest:: project pointer not initialsed" );

	pTest->assertTrue(pTest->m_project->getDefaultTarget() == "build", "getDefaultTarget()" );
}


void	ProjectTest::testGetDescription(TestCase* a_test )
{
	ProjectTest*			pTest			= static_cast<ProjectTest*>(a_test );
	pTest->assertPre(pTest->m_project != 0x0, "ProjectTest:: project pointer not initialsed" );

	pTest->assertTrue(pTest->m_project->getDescription() == "unit-test build file", "getDescription()" );
}


void	ProjectTest::testGetName(TestCase* a_test )
{
	ProjectTest*			pTest			= static_cast<ProjectTest*>(a_test );
	pTest->assertPre(pTest->m_project != 0x0, "ProjectTest:: project pointer not initialsed" );

	pTest->assertTrue(pTest->m_project->getName() == "test", "getName()" );
}
